<?php
namespace Modules\ShopApi\Models;
use Modules\AuthAdmin\Services\CommonService;
use Modules\AuthAdmin\Models\AuthImage as AuthImageModel;
use Modules\ShopApi\Models\ShopProductFootprint as ShopProductFootprintModel;
use Modules\ShopApi\Models\ShopProductCollection as ShopProductCollectionModel;
use Modules\ShopApi\Services\ShopUserService;
class ShopProduct extends BaseApiModel
{	 
	/**
	 * @name  关联商品图片   一对一
	 */
	 public function image_one()
    {
        return $this->hasOne('Modules\AuthAdmin\Models\AuthImage','id','image_id');
    }
	/**
     * @name  关联sku
     */
    public function product_spec_to()
    {
        return $this->hasMany('Modules\ShopApi\Models\ShopProductSpec','product_id','id');
    }
	/**
     * @name  商品列表
     */
	public function get_product($data){
		$model = $this->query();
		$model = $model->where('status',1);
		if(isset($data['key']) && $data['key'] != ''){
			$model = $model->where('name','like','%' . $data['key'] . '%');
		}
		if(isset($data['sales']) && $data['sales'] == 1){
			$model = $model->orderBy('sales','desc');
		}
		if(isset($data['isMoney']) && $data['isMoney'] == 1){
			$model = $model->orderBy('money','desc');
		}
		if(isset($data['product_type_id']) && $data['product_type_id']){
			$model = $model->where('product_type_id',$data['product_type_id']);
		}
		$list = $model->select('id','name','image_id','money','sales','bazaar_money')
				->with([
					'image_one'=>function($query){
						$query->select('id','url','open');  
					}
				])
				->orderBy('sort','asc')
				->orderBy('id','desc')
				->paginate(10)
				->toArray();		
		$list['data'] = $this->common_image_one($list['data']);
		return $this->api_success(['data'=>[
			'data'			=> $list['data'],
			'last_page'		=> $list['last_page'],
			'per_page'		=> $list['per_page'],
			'current_page'	=> $list['current_page'],
			'total' 		=> $list['total']
		]]);			
	}
	/**
     * @name  商品详情
     */
	public function get_product_detail($id){
		$user_info = (new ShopUserService())->my();
		$info = $this->select('id','name','images','attrbute_list','content','specs_status','sales','image_id')
				->with([
					'product_spec_to'=>function($query){
						$query->select('id','product_id','money','bazaar_money','postage','stock','sku');
					},
					'image_one'=>function($query){
						$query->select('id','url','open');  
					}
				])
				->where('id',$id)
				->first()
				->toArray();	
			
		if($info['specs_status'] == 1){
			$info['attrbute_list'] = json_decode($info['attrbute_list'],true);
		}
		$info = $this->images_list($info);
		$info['collection_status'] = 0;
		if($user_info){
			$footprint_id = ShopProductFootprintModel::where(['user_id'=>$user_info['id'],'product_id'=>$id])->value('id');
			if($footprint_id){
				ShopProductFootprintModel::where(['id'=>$footprint_id])->update([
					'created_at'=>date('Y-m-d H:i:s')
				]);
			}else{
				ShopProductFootprintModel::insert([
					'user_id'=>$user_info['id'],
					'product_id'=>$id,
					'created_at'=>date('Y-m-d H:i:s')
				]);
			}
			$info['collection_status'] = ShopProductCollectionModel::where(['user_id'=>$user_info['id'],'product_id'=>$id,'status'=>1])->count()?1:0;
		}	
		return $this->api_success(['data'=>$info]);	
	}
	/**
     * @name  图片处理
     */
	 private function images_list($info){
		$CommonService = new CommonService();
		$http = $CommonService->get_http_type();
		$info['content'] = $CommonService->replace_pic_url($info['content']);
		if($info['image_one'] && $info['image_one']['open'] == 1){
			$info['image_url'] = $http . $info['image_one']['url'];
		}else{
			$info['image_url'] = $info['image_one']['url'];
		}
		$info['images_arr'] = [];
		if($info['images']){
			$info['images'] = AuthImageModel::whereIn('id',explode('|',$info['images']))->select('id','url','open')->get();	
			if($info['images']){
				$info['images'] = $info['images']->toArray();
				foreach($info['images'] as $k=>$v){
					if($v['open'] == 1){
						$info['images_arr'][] = $http . $v['url'];
					}else{
						$info['images_arr'][] = $v['url'];
					}
				}
			}
		}
		unset($info['images'],$info['image_one']);
		return $info;
	 }
	/**
	 * @name  商品收藏
	 */
	public function product_collection($id){
		$user_info = (new ShopUserService())->my();
		$collection_info = ShopProductCollectionModel::where(['user_id'=>$user_info['id'],'product_id'=>$id])->select('id','status')->first();
		$status = 1;
		if($collection_info){
			$collection_info = $collection_info->toArray();
			$status = $collection_info['status']==1?0:1;
			$res = ShopProductCollectionModel::where(['id'=>$collection_info['id']])->update([
				'updated_at'=>date('Y-m-d H:i:s'),
				'created_at'=>date('Y-m-d H:i:s'),
				'status'=>$status
			]);
		}else{
			$res = ShopProductCollectionModel::insert([
				'user_id'=>$user_info['id'],
				'product_id'=>$id,
				'status'=>$status,
				'created_at'=>date('Y-m-d H:i:s')
			]);
		}
		if($res!==false){
			return $this->api_success(['msg'=>$status?'收藏成功！':'取消收藏成功！']);	
		}else{
			return $this->error();
		}
	}
	/**
	 * @name  获取商品小程序码
	 */
	public function app_code($id){
		$ShopUserService = new ShopUserService();
		return $this->api_success(['data'=>[
			'code_url'=>$ShopUserService->get_app_code('pages/product_detail/product_detail?id='.$id),
			'icon_url'=>(new CommonService())->get_http_type().'/upload/images/shop/hch-hyj.png'
		]]);
	}
}
